/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui.list;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;

public class DefaultAdvancedListCellRenderer extends DefaultListCellRenderer 
		implements AdvancedListCellRenderer {
	
	private static final long serialVersionUID = -3794198656466320713L;
	
	private int indexToHighlight = -1;
	private boolean highligtAllowed = true;
	private Color highlightColor = DEFAULT_HIGHLIGHT_COLOR;
	private Color highlightForegroundColor = null;
	private Color stripesColor = DEFAULT_STRIPES_COLOR;
	
	public DefaultAdvancedListCellRenderer() {
		setBorder(BorderFactory.createEmptyBorder(3, 6, 3, 6));
	}

	@Override
	public Component getListCellRendererComponent(JList list, Object value, 
			int index, boolean isSelected, boolean cellHasFocus) {
		
		super.getListCellRendererComponent(list, 
				value, 
				index, 
				isSelected, 
				cellHasFocus);
		
		if (!isSelected) {
			if (index % 2 == 1) {
				setBackground(getStripes());
			} else {
				setBackground(list.getBackground());
			}
		}
		
		if(index == getHighlightedIndex() && getHighlightedIndex() >= 0 &&
				isIndexHighlightAllowed()) {
			if(getHighlightForeground() != null) {
				setForeground(getForeground());
			}
			setBackground(getHighlightBackground());
		}
		
		return this;
	}
	
	@Override
	public void mouseEntered(MouseEvent e) {
		mouseMoved(e);
	}
	
	@Override
	public void mouseExited(MouseEvent e) {
		if(!(e.getComponent() instanceof JList)) {
			return;
		}
		
		boolean doRepain = getHighlightedIndex() != -1;
		setHighlightedIndex(-1);
		if(doRepain) {
			((JList)e.getComponent()).repaint();
		}
	}
	
	@Override
	public void mouseMoved(MouseEvent e) {
		if(!(e.getComponent() instanceof JList)) {
			return;
		}
		
		JList list = (JList) e.getComponent();
		
		int mouseOverIndex = list.locationToIndex(e.getPoint());
		if(mouseOverIndex < 0) {
			return;
		}
		if(!list.getCellBounds(mouseOverIndex, mouseOverIndex).contains(
				e.getPoint())) {
			mouseOverIndex = -1;
		}
		
		boolean doRepain = getHighlightedIndex() != mouseOverIndex;
		setHighlightedIndex(mouseOverIndex);
		if(doRepain) {
			list.repaint();
		}
	}
	
	@Override
	public void setHighlightedIndex(int index) {
		indexToHighlight = index;
	}
	@Override
	public int getHighlightedIndex() {
		return indexToHighlight;
	}
	
	@Override
	public void setStripes(Color stripesColor) {
		this.stripesColor = stripesColor;
	}
	@Override
	public Color getStripes() {
		return stripesColor;
	}
	
	@Override
	public void setHighlightBackground(Color highlightColor) {
		this.highlightColor = highlightColor;
	}
	@Override
	public Color getHighlightBackground() {
		return highlightColor;
	}
	
	@Override
	public Color getHighlightForeground() {
		return highlightForegroundColor;
	}
	@Override
	public void setHighlightForeground(Color highlightColor) {
		highlightForegroundColor = highlightColor;
	}
	
	@Override
	public boolean isIndexHighlightAllowed() {
		return highligtAllowed;
	}
	@Override
	public void setIndexHighlightAllow(boolean b) {
		highligtAllowed = b;
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
	}
	@Override
	public void mousePressed(MouseEvent e) {
	}
	@Override
	public void mouseReleased(MouseEvent e) {
	}
	@Override
	public void mouseDragged(MouseEvent e) {
	}
}
